package com.mt.roll.wms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mt.roll.core.api.Assert;
import com.mt.roll.core.base.BaseService;
import com.mt.roll.core.dal.entity.Area;
import com.mt.roll.core.dal.entity.Location;
import com.mt.roll.core.dal.entity.Shelf;
import com.mt.roll.core.dal.entity.WorkOrder;
import com.mt.roll.core.dal.service.LocationServiceBiz;
import com.mt.roll.core.dal.service.ShelfServiceBiz;
import com.mt.roll.core.params.IdParam;
import com.mt.roll.core.vo.IdVo;
import com.mt.roll.core.vo.PageVo;
import com.mt.roll.core.vo.R;
import com.mt.roll.wms.mapper.AreaLocationVoMapper;
import com.mt.roll.wms.mapper.AreaShelfVoMapper;
import com.mt.roll.wms.params.*;
import com.mt.roll.wms.service.AreaLocationService;
import com.mt.roll.wms.vo.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @auther yanyang
 * @date 2021/11/29 10:56
 */

@Service
@Transactional
public class AreaLocationServiceImpl extends BaseService implements AreaLocationService {

    @Autowired
    private LocationServiceBiz locationServiceBiz;

    @Autowired
    private AreaLocationVoMapper areaLocationVoMapper;

    @Override
    public R<PageVo<AreaLocationVo>> locationByShelf(AreaShelfQueryParam areaShelfQueryParam) {
        IPage<AreaLocationVo> page = areaLocationVoMapper.locationByShelf(new Page<>(areaShelfQueryParam.getCurrent(),areaShelfQueryParam.getSize()), areaShelfQueryParam);
        return successful(new PageVo<>(page, AreaLocationVo.class));
    }

    @Override
    public R<IdVo> addLocation(AreaLocationParam areaLocationParam) {
        QueryWrapper<Location> wrapper = new QueryWrapper<>();
        wrapper.eq(Location.CODE, areaLocationParam.getCode());
        Assert.eqZero(locationServiceBiz.count(wrapper), "已经存在，请勿重复添加");
        Location location = new Location();
        BeanUtils.copyProperties(areaLocationParam, location);
        setCommonField(location);
        locationServiceBiz.save(location);
        return successful(IdVo.builder().id(location.getId()).build());
    }

    @Override
    public R<AreaLocationVo> locationById(IdParam idParam) {
        Location location = locationServiceBiz.getById(idParam.getId());
        Assert.notNull("详情不存在", location);
        AreaLocationVo areaLocationVo = areaLocationVoMapper.locationById(idParam.getId());
        return successful(areaLocationVo);
    }

    @Override
    public R<IdVo> updateLocation(AreaLocationParam areaLocationParam) {
        Location location = locationServiceBiz.getById(areaLocationParam.getId());
        //修改信息需要检验是否已存在
        if (!location.getCode().equals(areaLocationParam.getCode())) {
            QueryWrapper<Location> wrapper = new QueryWrapper<>();
            wrapper.eq(location.CODE, areaLocationParam.getCode());
            Assert.eqZero(locationServiceBiz.count(wrapper),"该库位已经存在");
        }
        Location updateLocation = new Location();
        BeanUtils.copyProperties(areaLocationParam, updateLocation);
        setUpdateCommonField(updateLocation);
        locationServiceBiz.updateById(updateLocation);
        return successful(IdVo.builder().id(location.getId()).build());
    }

    @Override
    public R<IdVo> deleteLocation(IdParam idParam) {
        Location location = locationServiceBiz.getById(idParam.getId());
        if (location == null) {
            return failed("库位不存在");
        }
        locationServiceBiz.removeById(idParam.getId());
        return successful(IdVo.builder().id(location.getId()).build());
    }
}
